<?php

namespace app\api\controller\sys;

use app\common\controller\ApiBasic;
use app\common\service\sys\UserService;

class User extends ApiBasic
{
    // 初始化
    protected function initialize()
    {
        parent::initialize();
        // 当前控制器服务实例化
        $this->service = new UserService();
    }

    /**
     * 登出
     */
    public function logout()
    {
        $result = UserService::toLogout($this->user['id']);
        return json($result);
    }

    /**
     * 修改密码
     */
    public function editpwd()
    {
        if (request()->isPost()) {
            $id = $this->user['id']; //这里id不是靠传入的，是取自判定 token的
            if (!$id) {
                return _jerr(1026);
            }
            $data = $this->request->post();
            if (isset($data['password']) && empty($data['password'])) {
                return _jerr(1022);
            }
            if (isset($data['newPwd']) && empty($data['newPwd'])) {
                return _jerr(1022);
            }
            if (isset($data['rePwd']) && empty($data['rePwd'])) {
                return _jerr(1022);
            }
            if ($data['rePwd'] !== $data['newPwd']) {
                return _jerr(1022);
            }
            return json($this->service->chengePwd($id, $data['password'], $data['newPwd']));
        }
        return _jerr(1000);
    }


    /**
     * 编辑
     */
    public function edit()
    {
        $data = input('param.');
        url_decode($data);
        $data['id'] = $this->user['id']; //这里id不是靠传入的，是取自判定 token的
        if (!isset($data['id']) || empty($data['id'])) {
            return _jerr(1022);
        }
        if ($this->request->isPost()) {
            return json($this->service->updateFieldApi($data));
        }
        return _jerr(1000);
    }

    public function getuserinfo()
    {
        if ($this->request->isGet()) {
            $id = $this->user['id'];
            if (!$id) {
                return _jerr(1026);
            }
            return json($this->service->getuserinfo($id));
        }
        return _jerr(1000);
    }
}
